package cn.cq.validator;

import cn.cq.annotation.IsMobile;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.validation.ValidationUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean require = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {

        this.require = constraintAnnotation.require();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (require){
            return Validator.isMobile(value);
        } else if (StrUtil.isNotEmpty(value)) {
            return Validator.isMobile(value);
        }else{
            return true;
        }
    }
}